class BreedsController < ApplicationController
  # GET /breeds
  # GET /breeds.xml
  def index
    @breeds = Breed.search(params[:search], params[:page])
    
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @breeds }
    end
  end
  
  # GET /breeds/1
  # GET /breeds/1.xml
  def show
    @breed = Breed.find(params[:id])
    
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @breed }
    end
  end
  
  # GET /breeds/new
  # GET /breeds/new.xml
  def new
    if authorize
      @breed = Breed.new
      
      respond_to do |format|
        format.html # new.html.erb
        format.xml  { render :xml => @breed }
      end
    end
  end
  
  # GET /breeds/1/edit
  def edit
    if authorize
      @breed = Breed.find(params[:id])
    end
  end
  
  # POST /breeds
  # POST /breeds.xml
  def create
    @breed = Breed.new(params[:breed])
    
    respond_to do |format|
      if @breed.save
        flash[:notice] = 'Breed was successfully created.'
        format.html { redirect_to(@breed) }
        format.xml  { render :xml => @breed, :status => :created, :location => @breed }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @breed.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  # PUT /breeds/1
  # PUT /breeds/1.xml
  def update
    @breed = Breed.find(params[:id])
    
    respond_to do |format|
      if @breed.update_attributes(params[:breed])
        flash[:notice] = 'Breed was successfully updated.'
        format.html { redirect_to(@breed) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @breed.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  # DELETE /breeds/1
  # DELETE /breeds/1.xml
  def destroy
    if authorize
      @breed = Breed.find(params[:id])
      @breed.destroy
      
      respond_to do |format|
        format.html { redirect_to(breeds_url) }
        format.xml  { head :ok }
      end
    end
  end
  protected
  def authorize
    unless session[:user_id]
      flash[:notice] = "<div class='invisible'>Por favor identifiquese<div>"
      redirect_to :controller => 'admin', :action => 'login', :class => 'nyroModal', :rev=>'gal'
    else
      user = User.find(session[:user_id])
      if user.user_tipe_id == 1
        return true        
      else
        return false
      end
    end
  end
end
